package com.hui.admin.controller;

import com.hui.admin.annotation.Log;
import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.dto.request.*;
import com.hui.admin.dto.response.UserPageResponseDTO;
import com.hui.admin.dto.response.UserProfileResponseDTO;
import com.hui.admin.service.UserProfileService;
import com.hui.admin.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    private final UserProfileService userProfileService;

    /**
     * 新增用户
     */
    @PostMapping
    @Log("新增用户")
    public Result<Void> addUser(@Valid @RequestBody UserSaveRequestDTO userSaveRequestDTO) {
        userService.addUser(userSaveRequestDTO);
        return Results.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @Log("删除用户")
    public Result<Void> deleteUserById(@PathVariable Long id) {
        userService.deleteUserById(id);
        return Results.success();
    }

    /**
     * 分页查询
     */
    @GetMapping
    public Result<UserPageResponseDTO> getUserPage(UserPageRequestDTO requestDTO) {
        return Results.success(userService.getUserPage(requestDTO));
    }

    /**
     * 修改用户
     */
    @PutMapping
    @Log("修改用户")
    public Result<Void> updateUser(@Valid @RequestBody UserSaveRequestDTO requestDTO) {
        userService.updateUser(requestDTO);
        return Results.success();
    }

    /**
     * 修改用户状态
     */
    @PutMapping("/status")
    @Log("修改用户状态")
    public Result<Void> updateUserStatus(@RequestParam String username, @RequestParam Integer status) {
        userService.updateUserStatus(username, status);
        return Results.success();
    }

    /**
     * 批量删除用户
     */
    @Log("批量删除用户")
    @DeleteMapping("/batch")
    public Result<Void> deleteUsers(@RequestBody Long[] ids) {
        userService.deleteUsers(ids);
        return Results.success();
    }

    /**
     * 导出用户数据
     */
    @Log("导出用户数据")
    @PostMapping("/export")
    public void exportUsers(HttpServletResponse response,
                            @RequestBody UserExportRequestDTO requestDTO) throws IOException {
        userService.exportUsers(response, requestDTO);
    }

    /**
     * 导入用户数据
     */
    @Log("导入用户数据")
    @PostMapping("/import")
    public Result<Void> importUsers(@RequestParam("file") MultipartFile file) {
        userService.importUsers(file);
        return Results.success();
    }

    /**
     * 修改用户密码
     */
    @Log("修改用户密码")
    @PutMapping("/password")
    public Result<Void> updateUserPassword(@Valid @RequestBody UserChangePasswordRequestDTO requestDTO) {
        userService.updateUserPassword(requestDTO);
        return Results.success();
    }

    /**
     * 修改用户信息
     */
    @Log("修改用户信息")
    @PutMapping("/profile")
    public Result<Void> updateUserProfile(@RequestBody UserProfileRequestDTO requestDTO) {
        userProfileService.updateUserProfile(requestDTO);
        return Results.success();
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/profile")
    public Result<UserProfileResponseDTO> getUserProfileByUserId(@RequestParam Long userId) {
        return Results.success(userProfileService.getUserProfileById(userId));
    }
}
